import { InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { SpawnLayoutConfig } from "GameLoop/WorldState/EntityManager/Entitys/room/spawn/spawnlayout";
import { PositionHelper } from "utils/position/PositionHelper";
import { StationNetwork } from "GameLoop/Path/StationNetwork";




/**
 * 填充ext的闲暇工作
 */ 
 
export class Carryer<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     * 搬运工行为,先做简化行为。运输的两端都有container
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
        // global.Log.Info(`${arg.entity.name}`);
        const creep = arg.entity;
        const spawnentity =  arg.entity.ownerRoom().getEntity("SpawnEntity");
        const coreEntity = arg.entity.ownerRoom().getEntity("CoreEntity");
        // 1.先填充spawn的container
        if(coreEntity.Link && spawnentity.Link)
        {
            // 核心合spawn都有link了就不用两边搬了
            
            return []
        }
        
        if(creep.store.getUsedCapacity(RESOURCE_ENERGY)>0)
        {
            creep.memory.state = 1;

        }
        else  
        {
            creep.memory.state = 0;
            // 绝不空载
            creep.memory.targetId=undefined;
        }
        if(creep.memory.state)
        {
            const getTransferObject=( )=>
            {
                // 运
               
                const containers = spawnentity.getStoreObjects({resourceType:RESOURCE_ENERGY,amount:2000});
                if(containers.length>0)
                {
                    return containers[0]
                }
                const controller  =       arg.entity.ownerRoom().controller!;
                // if(coreEntity.resourceCount(RESOURCE_ENERGY)>120000  )
                {
                    for(const e of  controller.getContainer())
                    {
                        if(e.store.getFreeCapacity(RESOURCE_ENERGY)>100)
                        {
                            return e;
                        }
                    }
                }
                const stores = coreEntity.getStores({resourceType:RESOURCE_ENERGY,amount:2000});
                if(coreEntity.resourceCount(RESOURCE_ENERGY)<120000 && stores.length>0)
                {
                    return stores[0]
                }
                return false;
            }
            if(!creep.memory.targetId)
            {
                const obj = getTransferObject();
                if(obj)
                {
                    creep.memory.targetId = obj.id;
                }
            }
            if(creep.memory.targetId)
            {
                const store = Game.getObjectById(creep.memory.targetId) as AnyStoreStructure;
                if(store && store.store.getFreeCapacity(RESOURCE_ENERGY)>0)
                {
                    return [ActionArgHelper.actionTransfer(creep,store)];
                }
                creep.memory.targetId=undefined;
            }
            
        }
        else
        {
            const spawnentity =  arg.entity.ownerRoom().getEntity("SpawnEntity");
            const containers = spawnentity.getWithdrawObjects({resourceType:RESOURCE_ENERGY,amount:2000});
            if(containers.length>0&& spawnentity.Link && spawnentity.Link.store.getUsedCapacity(RESOURCE_ENERGY)>100)
            {
                for(const e of containers)
                {
                    return [ActionArgHelper.actionWithdraw(creep,e)]
                }
            }

            const   taskobj  =  coreEntity.getTakeStores({resourceType:RESOURCE_ENERGY,amount:130000})
            if(taskobj.length>0)
            {
                return [ActionArgHelper.actionWithdraw(creep,taskobj[0])]
            }
            // 2.填充控制器的container
            // 取
            if(containers.length && containers.every(e=>e.store.getUsedCapacity(RESOURCE_ENERGY)==0))
            {
                const mine  =       arg.entity.ownerRoom().getEntitys("InnerMineEntity");
                for(const e of mine)
                {
                    const c = e.getContainer();
                    if(c&& c.store.getUsedCapacity(RESOURCE_ENERGY)>creep.store.getFreeCapacity())
                    {
                        return [ActionArgHelper.actionWithdraw(creep,c,[{resourceType:RESOURCE_ENERGY}])]
                    }
                }
            }
        }
        return []

    }
}
